﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 美妆产品管理系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int flag = 1;//1为主窗体，2为管理员窗体
        public static bool Aflag = false;//false未登录，true已登录
        public static string GoodID = "";
        void showAllProduct()
        {
            DB.GetCn();
            string str = "select * from Goods ";
            SqlCommand cmd = new SqlCommand(str, DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                int index = dgvPriduct.Rows.Add();
                dgvPriduct.Rows[index].Cells[0].Value = rdr[0];
                dgvPriduct.Rows[index].Cells[1].Value = rdr[1];
                dgvPriduct.Rows[index].Cells[2].Value = rdr[2];
                dgvPriduct.Rows[index].Cells[3].Value = rdr[3];
                dgvPriduct.Rows[index].Cells[4].Value = rdr[4];
                dgvPriduct.Rows[index].Cells[5].Value = rdr[5];
                dgvPriduct.Rows[index].Cells[6].Value = rdr[8];
                try
                {
                    Image imageCloum = Image.FromFile(Application.StartupPath + rdr[7]);
                    dgvPriduct.Rows[index].Cells["Column8"].Value = imageCloum;
                }
                catch
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.jpg");
                    dgvPriduct.Rows[index].Cells["Column8"].Value = imageColum;
                }
            }
            rdr.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            label7.Parent = label6;
            dgvPriduct.RowTemplate.Height = 90;
            showAllProduct();
        }

        private void btnshowall_Click(object sender, EventArgs e)
        {
            dgvPriduct.Rows.Clear();
            showAllProduct();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnSearch1_Click(object sender, EventArgs e)//按商品名称关键字查询
        {
            if (txtPName.Text == "")
            {
                MessageBox.Show("请输入查询关键字");
            }
            else
            {
                dgvPriduct.Rows.Clear();
                DB.GetCn();
                string str = "select * from Goods where Goods_Name like '%" + txtPName.Text + "%'";
                SqlCommand cmd = new SqlCommand(str, DB.cn);
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    int index = dgvPriduct.Rows.Add();
                    dgvPriduct.Rows[index].Cells[0].Value = rdr[0];
                    dgvPriduct.Rows[index].Cells[1].Value = rdr[1];
                    dgvPriduct.Rows[index].Cells[2].Value = rdr[2];
                    dgvPriduct.Rows[index].Cells[3].Value = rdr[3];
                    dgvPriduct.Rows[index].Cells[4].Value = rdr[4];
                    dgvPriduct.Rows[index].Cells[5].Value = rdr[5];
                    dgvPriduct.Rows[index].Cells[6].Value = rdr[8];
                    try
                    {
                        Image imageCloum = Image.FromFile(Application.StartupPath + rdr[7]);
                        dgvPriduct.Rows[index].Cells["Column8"].Value = imageCloum;
                    }
                    catch
                    {
                        Image imageColum = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.jpg");
                        dgvPriduct.Rows[index].Cells["Column8"].Value = imageColum;
                    }
                }
                rdr.Close();
            }
        }

        private void btnSearch2_Click(object sender, EventArgs e)//按照商品价格区间查询
        {
            dgvPriduct.Rows.Clear();
            string str = "";
            DB.GetCn();
            int i = comboBox1.SelectedIndex;
            switch (i)
            {
                case 0:
                    str = "Unit_Price<=50";
                    break;
                case 1:
                    str = "Unit_Price>50 and Unit_Price<=100";
                    break;
                case 2:
                    str = "Unit_Price>100 and Unit_Price<=300";
                    break;
                case 3:
                    str = "Unit_Price>300 and Unit_Price<=500";
                    break;
                case 4:
                    str = "Unit_Price>500 and Unit_Price<=800";
                    break;
                case 5:
                    str = "Unit_Price>800 and Unit_Price<=1000";
                    break;
                default:
                    str = "Unit_Price>1000";
                    break;
            }
            string sdr = "select * from  Goods where "+str +"";
            SqlCommand cmd = new SqlCommand(sdr, DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                int index = dgvPriduct.Rows.Add();
                dgvPriduct.Rows[index].Cells[0].Value = rdr[0];
                dgvPriduct.Rows[index].Cells[1].Value = rdr[1];
                dgvPriduct.Rows[index].Cells[2].Value = rdr[2];
                dgvPriduct.Rows[index].Cells[3].Value = rdr[3];
                dgvPriduct.Rows[index].Cells[4].Value = rdr[4];
                dgvPriduct.Rows[index].Cells[5].Value = rdr[5];
                dgvPriduct.Rows[index].Cells[6].Value = rdr[8];
                try
                {
                    Image imageCloum = Image.FromFile(Application.StartupPath + rdr[7]);
                    dgvPriduct.Rows[index].Cells["Column8"].Value = imageCloum;
                }
                catch
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.jpg");
                    dgvPriduct.Rows[index].Cells["Column8"].Value = imageColum;
                }
            }
            rdr.Close();

        }
        

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnregister_Click(object sender, EventArgs e)
        {
            Register r1 = new Register();
            r1.ShowDialog();
        }

        private void btnLand_Click(object sender, EventArgs e)
        {
            this.Hide();
            Login I1 = new Login();
            I1.ShowDialog();
            if (flag == 1)
            {
                this.Visible = true;
                label5.Text = Login.username;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label7.Top = label6.Height / 2;
            if (label7.Left < -label7.Width)
            {
                label7.Left = label6.Width;
            }
            label7.Left -= 10;
        }

        private void dgvPriduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            //如果是第8列，查看商品的详细信息
            if (CIndex == 8)
            {
                //确定是第几行
                int rowindex = e.RowIndex;
                GoodID = dgvPriduct.Rows[rowindex].Cells[0].Value.ToString();
                TGood t1 = new TGood();
                t1.ShowDialog();
            }
            //如果是第9列，查看购物车
            if (CIndex == 9)
            {
                if (Aflag == false)
                {
                    MessageBox.Show("请先登录");
                    Login l1 = new Login();
                    l1.ShowDialog();
                }
                else
                {
                    //确定第几行
                    int rowidex = e.RowIndex;
                    GoodID = dgvPriduct.Rows[rowidex].Cells[0].Value.ToString();
                    if (Convert.ToInt32(dgvPriduct.Rows[rowidex].Cells[5].Value) < 1)
                    {
                        MessageBox.Show("该商品暂时缺货");
                    }
                    else
                    {
                        ShopCart s1 = new ShopCart();
                        s1.ShowDialog();
                    }
                }
            }
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void btnorder_Click(object sender, EventArgs e)
        {
            if (Aflag == false)
            {
                MessageBox.Show("请先登录");
                Login l1 = new Login();
                l1.ShowDialog();
            }
            else
            {
                Order_info or1 = new Order_info();
                or1.ShowDialog();
            }
        }
    }
}
